{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#列表基础\" data-toc-modified-id=\"列表基础-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>列表基础</a></span></li><li><span><a href=\"#列表操作\" data-toc-modified-id=\"列表操作-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>列表操作</a></span><ul class=\"toc-item\"><li><span><a href=\"#for循环参与下对列表的操作\" data-toc-modified-id=\"for循环参与下对列表的操作-2.1\"><span class=\"toc-item-num\">2.1&nbsp;&nbsp;</span>for循环参与下对列表的操作</a></span></li><li><span><a href=\"#切片\" data-toc-modified-id=\"切片-2.2\"><span class=\"toc-item-num\">2.2&nbsp;&nbsp;</span>切片</a></span></li><li><span><a href=\"#复制列表\" data-toc-modified-id=\"复制列表-2.3\"><span class=\"toc-item-num\">2.3&nbsp;&nbsp;</span>复制列表</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表基础\n",
    "*列表的命令一般用复数表示，例如`names` `students`*\n",
    "- 元素构建\n",
    "    1. 创建一个列表并打印出来 `list = ['', '', '']`\n",
    "    2. 访问列表元素,整洁干净 `list[i]` i为index其中i <= n-1\n",
    "    3. 使用字符串操作函数 `list[i].title()` 使首字母大写\n",
    "    4. 索引(index)的使用，正序排列 `list[i]` 为正数第（i-1）个元素，`list[-i]` 则表示倒数第(i)个元素\n",
    "    5. 使用列表里面的值\n",
    "    6. 练习题，为不同的朋友表达问候语,循环 `for i in range()` 测量列表的长度用`len(lsit)` \n",
    "- 元素操作\n",
    "    1. 元素修改 `list[i] = 'string/number/eles'` \n",
    "    2. 元素添加\n",
    "        1. 列表末尾添加元素 `list.append('string/number/eles')` 一般使用`append（）` 来在一个新建的空列表后面一个个添加元素 \n",
    "        2. 列表中间添加元素 `list.insert（i, 'string/number/eles'）` 则此位置加入元素之后后面所有元素的index向右位移一位\n",
    "    3. 元素删除\n",
    "        0. `del` 和 `pop` 的作用一样，用法差别在于是否使用这个被删除的元素，若果不是用则使用 `del` 来处理\n",
    "        1. 使用 `del list[i]` 删除列表中的某一个index下的元素\n",
    "        2. 使用 `pop()` 来删除元素，其中 `pop()` 为默认弹出栈顶元素即列表中的最后一个元素赋给一个变量，相当于 `pop(i-1)` ，而使用 `pop(i)` 则是弹出第（i）个元素存到某个变量之中\n",
    "        3. 根据元素的值来删除元素，方法为 `list.remove('string/number/')` `remove()` 只能删除列表之中第一个出现的值，若列表后续还有则不会删除，可以使用循环来进行删除后续元素\n",
    "- 练习题\n",
    "    1. 创建三个嘉宾名单的列表并打印出来提出邀请\n",
    "    2. 修改名单\n",
    "        1. `print` 出哪个嘉宾不接受邀请\n",
    "        2. 修改名单替换掉新加入的嘉宾\n",
    "        3. 再次打印邀请信息\n",
    "    3. 找到了一个大餐桌，需要增加三位宾客\n",
    "        1. `print` 出更大的餐桌\n",
    "        2. `insert()` 将一个新宾客加到开头\n",
    "        3. `insert()` 将另一个宾客加到中间\n",
    "        4. `append()` 将最后的宾客加到结尾处\n",
    "        5. 再次打印邀请信息\n",
    "    4. 餐桌没有到达，只能邀请两位嘉宾\n",
    "        1. `print` 出这个消息\n",
    "        2. `pop()` 出嘉宾只剩下两位，每弹出一次发送一条消息\n",
    "        3. 对余下的两位嘉宾发送消息\n",
    "        4. 使用 `del` 将列表删除为空并打印\n",
    "- 列表组织\n",
    "    1. 使用 `sort()` 对列表进行按字母排序，默认按照首字母升序，参数 `reverse=True` 则表示按首字母降序排列(永久排序)\n",
    "    2. 使用 `sorted()` 对列表进行按字母排序，默认按照字母升序排列， 参数 `reverse=True` 时按照降序排列（临时排序）\n",
    "    3. 倒着打印列表*注意不是倒序*，函数如 `list.reverse()` ，计算列表长度 `len(list)` \n",
    "- 练习题\n",
    "    1. 创建旅游城市列表\n",
    "        1. 乱序存放列表\n",
    "        2. 打印该列表\n",
    "        3. 使用 `sorted()` 来排序列表并打印\n",
    "        4. 再次打印看列表状态\n",
    "        5. 使用 `sorted()` 来按字母降序打印该列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['tom', 'Gray', 'Harry', 'Adama']\n",
      "tom\n",
      "Tom\n",
      "Harry\n",
      "Adama\n",
      "My friend is Gray.\n",
      "Tom Hello, I will tell you that list is a simple data structe.\n",
      "Gray Hello, I will tell you that list is a simple data structe.\n",
      "Harry Hello, I will tell you that list is a simple data structe.\n",
      "Adama Hello, I will tell you that list is a simple data structe.\n"
     ]
    }
   ],
   "source": [
    "names = ['tom', 'Gray', 'Harry', 'Adama']\n",
    "#1\n",
    "print(names)\n",
    "#2\n",
    "print(names[0])\n",
    "#3\n",
    "print(names[0].title())\n",
    "#4\n",
    "print(names[2])\n",
    "print(names[-1])\n",
    "#5\n",
    "message = \"My friend is \" + names[1].title() + \".\"\n",
    "print(message)\n",
    "#6\n",
    "message_6 = \"Hello, I will tell you that list is a simple data structe.\"\n",
    "for i in range(len(names)):\n",
    "    print(names[i].title() + \" \" + message_6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['honda', 'yamaha', 'suzuki']\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'motors' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-b8005bc03222>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mmotors_1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'honda'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'yamaha'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'suzuki'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmotors_1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mmotors\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'ducati'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmotors\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"\\n\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'motors' is not defined"
     ]
    }
   ],
   "source": [
    "#1\n",
    "motors_1 = ['honda', 'yamaha', 'suzuki']\n",
    "print(motors_1)\n",
    "motors[1] = 'ducati'\n",
    "print(motors)\n",
    "print(\"\\n\")\n",
    "#2_1\n",
    "motors_2 = ['honda', 'yamaha', 'suzuki']\n",
    "motors_2.append('ducati')\n",
    "print(motors_2)\n",
    "print(\"\\n\")\n",
    "#2_1append()\n",
    "motors_2 = []\n",
    "motors_2.append('honda')\n",
    "motors_2.append('yamaha')\n",
    "motors_2.append('suzuki')\n",
    "print(motors_2)\n",
    "print(\"\\n\")\n",
    "#2_2 insert()\n",
    "motors_2 = ['honda', 'yamaha', 'suzuki']\n",
    "motors_2.insert(1, 'ducati')\n",
    "print(motors_2)\n",
    "print(\"\\n\")\n",
    "#3_1\n",
    "motors_3 = ['honda', 'yamaha', 'suzuki']\n",
    "print(motors_3)\n",
    "del motors_3[1]\n",
    "print(motors_3)\n",
    "print(\"\\n\")\n",
    "#3_2\n",
    "motors_3 = ['honda', 'yamaha', 'suzuki']\n",
    "print(motors_3)\n",
    "last_motor = motors_3.pop()\n",
    "print(motors_3)\n",
    "print(last_motor)\n",
    "motors_3 = ['honda', 'yamaha', 'suzuki']\n",
    "print(motors_3)\n",
    "i_motor = motors_3.pop(1)\n",
    "print(motors_3)\n",
    "print(i_motor)\n",
    "print(\"\\n\")\n",
    "#3_3\n",
    "motors_3 = ['honda', 'yamaha', 'suzuki']\n",
    "print(motors_3)\n",
    "motors_3.remove('yamaha')\n",
    "print(motors_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1 嘉宾列表创建\n",
    "names = ['Adam', 'SGD', 'Ggsy']\n",
    "message = \"邀请你吃晚饭\"\n",
    "for i in range(len(names)):\n",
    "    print(names[i] + \" \" + message + \"!\")\n",
    "print(\"----------------------\")\n",
    "#2 修改名单\n",
    "print(names[1] + \" 不能参加邀请！\")\n",
    "names[1] = 'Bob'\n",
    "for i in range(len(names)):\n",
    "    print(names[i] + \" \" + message + \"!\")\n",
    "print(\"----------------------\")\n",
    "#3 增删嘉宾\n",
    "print(\"找到了更大的餐桌\")\n",
    "names.insert(0, 'Tom')\n",
    "names.insert(1, 'Harry')\n",
    "names.append('Kate')\n",
    "for i in range(len(names)):\n",
    "    print(names[i] + \" \" + message + \"!\")\n",
    "print(\"----------------------\")\n",
    "# 删除嘉宾\n",
    "print(\"因餐桌没有及时到达，只能邀请两位宾客\")\n",
    "bad_message = \" 很遗憾没有邀请到你\"\n",
    "for i in range(len(names)-2):\n",
    "    pop_name = names.pop()\n",
    "    print(pop_name + \" \" + bad_message + \"!\")\n",
    "print(\"----------------------\")\n",
    "for i in range(len(names)):\n",
    "    print(names[i] + \" \" + message + \"!\")\n",
    "print(\"----------------------\")\n",
    "print(names)\n",
    "for i in range(len(names)):\n",
    "    del names[i-1]\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#4_1\n",
    "cars = ['bmw', 'audi', 'subaru', 'toyota']\n",
    "cars.sort()\n",
    "print(cars)\n",
    "cars.sort(reverse=True)\n",
    "print(cars)\n",
    "print(\"----------------------\")\n",
    "#4_2\n",
    "cars = ['bmw', 'audi', 'subaru', 'toyota']\n",
    "print(\"原始列表： \", cars)\n",
    "print(\"临时升序排列列表： \", sorted(cars))\n",
    "print(\"临时降序排列列表： \", sorted(cars, reverse=True))\n",
    "print(\"转换后列表恢复到原始状态\", cars)\n",
    "print(\"----------------------\")\n",
    "#4_3\n",
    "cars = ['bmw', 'audi', 'subaru', 'toyota']\n",
    "cars.reverse()\n",
    "print(cars)\n",
    "print(len(cars))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "citys = ['japan', 'us', 'hongkong', 'french', 'canada']\n",
    "print(citys)\n",
    "print(sorted(citys))\n",
    "print(citys)\n",
    "print(sorted(citys, reverse=True))\n",
    "print(citys)\n",
    "citys.reverse()\n",
    "print(citys)\n",
    "citys.reverse()\n",
    "print(citys)\n",
    "citys.sort()\n",
    "print(citys)\n",
    "citys.sort(reverse=True)\n",
    "print(citys)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表操作\n",
    "##### for循环参与下对列表的操作\n",
    "1. 遍历整个列表,并在循环中对每个元素进行操作\n",
    "2. `range()` 的使用,`range(1,5)`只会打印到4，配合`list()` 来创建数字列表,还可以指定步长\n",
    "3. 将1~10数字的平方存储到列表中\n",
    "4. 列表进行简单的统计\n",
    "5. **列表解析**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice, was a great trick!\n",
      "David, was a great trick!\n",
      "Carolina, was a great trick!\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "[1, 2, 3, 4, 5]\n",
      "[1, 3, 5, 7, 9]\n",
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n",
      "1\n",
      "35\n",
      "79\n",
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n"
     ]
    }
   ],
   "source": [
    "#1\n",
    "magicians = ['alice', 'david', 'carolina']\n",
    "for magician in magicians:\n",
    "    print(magician.title() + \", was a great trick!\")\n",
    "#2\n",
    "for value in range(1,5):\n",
    "    print(value)\n",
    "numbers = list(range(1,6))\n",
    "print(numbers)\n",
    "strib_numbers = list(range(1, 11, 2))\n",
    "print(strib_numbers)\n",
    "#3\n",
    "sqNumbers = []\n",
    "for i in range(1,11):\n",
    "    sqValue = i**2\n",
    "    sqNumbers.append(sqValue)\n",
    "print(sqNumbers)\n",
    "#4\n",
    "digits = [2,35,4,2,5,7,4,1,9,10]\n",
    "print(min(digits))\n",
    "print(max(digits))\n",
    "print(sum(digits))\n",
    "#5\n",
    "sqNumbers = [value**2 for value in range(1,11)]\n",
    "print(sqNumbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 切片\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['charhk', 'askjajd', 'cbewi']\n",
      "['askjajd', 'cbewi', 'dhiajl']\n",
      "['charhk', 'askjajd', 'cbewi', 'dhiajl']\n",
      "['cbewi', 'dhiajl', 'ekdh']\n",
      "['cbewi', 'dhiajl', 'ekdh']\n",
      "Charhk\n",
      "Askjajd\n",
      "Cbewi\n"
     ]
    }
   ],
   "source": [
    "players = ['charhk', 'askjajd', 'cbewi', 'dhiajl', 'ekdh']\n",
    "print(players[0:3])\n",
    "print(players[1:4])\n",
    "print(players[:4])\n",
    "print(players[2:])\n",
    "print(players[-3:])\n",
    "for player in players[:3]:\n",
    "    print(player.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 复制列表\n",
    "**列表复制的一个tip**\n",
    "首先，单纯的复制就是 `list2 = list1[:]` 这样的结果是 `list1` 和 `list2` 是两个不同的没有其他联系的列表，一个列表的改变另一个列表不会随之改变；\n",
    "而如果`list2 = list1` 这样的话实际上是在二者之间建立了一种映射关系，无论哪个列表改变，二者都会更新元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['pizza', 'falafel', 'carrot coke']\n",
      "['pizza', 'falafel', 'carrot coke', 'noodle'] \n",
      " ['pizza', 'falafel', 'carrot coke']\n",
      "['1', '2', '1'] \n",
      " ['1', '2', '1']\n",
      "['1', '2', '1', '3'] \n",
      " ['1', '2', '1', '3']\n"
     ]
    }
   ],
   "source": [
    "my_foods = ['pizza', 'falafel', 'carrot coke']\n",
    "friend_foods = my_foods[:]\n",
    "print(friend_foods)\n",
    "my_foods.append('noodle')\n",
    "print(my_foods, \"\\n\", friend_foods)\n",
    "\n",
    "list1 = ['1','2','1']\n",
    "list2 = list1\n",
    "print(list1, \"\\n\", list2)\n",
    "list2.append('3')\n",
    "print(list1, \"\\n\", list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "index = 0\n",
    "list_b = np.zeros((1, 3))\n",
    "list_a = [1,2,3]\n",
    "list_b[index, :] = list_a[:]\n",
    "print(list_b[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "195px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
